package icedot.library.common.base;

import icedot.library.common.utils.CommonDef;
import icedot.library.common.utils.StringUtils;

import java.util.Locale;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class Logger
{
    public static final int s_debug     = 1;  
    public static final int s_info      = 2;  
    public static final int s_warn      = 3;
    public static final int s_error     = 4;

    public static int  s_logLevel  = s_debug;  //默认的打印等级
    public static int s_debugLevel = s_debug;      //默认的打印等级

    /**
     * Toast提示信息
     * @param context
     * @param msg
     */
    public static void showHintMsg(Context context,String msg)
    {
        if(StringUtils.isNullOrEmpty(msg) || context == null)
        {
            return;
        }
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }

    public static void showHintMsgShort(Context context,String msg)
    {
        if(StringUtils.isNullOrEmpty(msg) || context == null)
        {
            return;
        }
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
    /**
     * 打印日志,使用指定的级别.
     * 
     * @param level
     *            如果设置的基本不存在就使用s_error
     * @param tag
     *            默认的标记为ExceptionMsg
     * @param msg
     *            内容
     */
    public static void showDebugMsg(int level, String tag, String msg)
    {
        StackTraceElement traceElement = Thread.currentThread().getStackTrace()[3];// 从堆栈信息中获取当前被调用的方法信息
        String logText = String.format(Locale.getDefault(), "%s %s:%d ",
                traceElement.getFileName(), traceElement.getMethodName(),
                traceElement.getLineNumber());
        if (StringUtils.isNullOrEmpty(tag))
        {
            tag = "IceDotHint";
        }
        if (level <= 0 || level > 4)
        {
            level = s_error;
        }
        printLog(level, tag, logText + msg);
    }

    /**
     * 打印异常日志
     * @param tag
     * @param msg
     */
    public static void errorMsg(String tag,String msg)
    {
        if(s_error < s_logLevel)
            return;
        StackTraceElement traceElement = Thread.currentThread().getStackTrace()[3];// 从堆栈信息中获取当前被调用的方法信息
        String logText = String.format(Locale.getDefault(), "%s %s:%d ",
                traceElement.getFileName(), traceElement.getMethodName(),
                traceElement.getLineNumber());
        if (StringUtils.isNullOrEmpty(tag))
        {
            tag = "ExceptionMsg";
        }
        printLog(s_error, tag, logText + msg);
    }

    /**
     * 打印异常日志
     * @param msg
     */
    public static void errorMsg(String msg)
    {
        if(s_error < s_logLevel)
            return;
        StackTraceElement traceElement = Thread.currentThread().getStackTrace()[3];// 从堆栈信息中获取当前被调用的方法信息
        String logText = String.format(Locale.getDefault(), "%s %s:%d ",
                traceElement.getFileName(), traceElement.getMethodName(),
                traceElement.getLineNumber());
        String tag = "ExceptionMsg";
        printLog(s_error, tag, logText + msg);
    }

    /**
     * 打印日志会更加s_showDebug值来判断是否打印
     * 
     * @param tag
     *            默认为DebugMsg
     * @param msg
     *            内容
     */
    public static void showDebugMsg(String tag, String msg)
    {
        if(s_debugLevel < s_logLevel)
            return;
        StackTraceElement traceElement = Thread.currentThread()
                .getStackTrace()[3];// 从堆栈信息中获取当前被调用的方法信息
        String logText = String.format(Locale.getDefault(), "%s %s:%d ",
                traceElement.getFileName(), traceElement.getMethodName(),
                traceElement.getLineNumber());
        if (StringUtils.isNullOrEmpty(tag))
        {
            tag = "IceDotHint";
        }
        printLog(s_debugLevel, tag, logText + msg);
    }
    
    public static void showDebugMsg(String msg)
    {
        if(s_debugLevel < s_logLevel)
            return;
        StackTraceElement traceElement = Thread.currentThread()
                .getStackTrace()[3];// 从堆栈信息中获取当前被调用的方法信息
        String logText = String.format(Locale.getDefault(), "%s %s:%d ",
                traceElement.getFileName(), traceElement.getMethodName(),
                traceElement.getLineNumber());
        printLog(s_debugLevel, "IceDotHint", logText + msg);
    }

    /**
     * 打印日志
     * 
     * @param level
     *            日志的等级
     * @param tag
     *            标记
     * @param msg
     *            内容
     */
    private static void printLog(int level, String tag, String msg)
    {
        if(CommonDef.s_debug)
        {
            tag = "IceDotHint";
        }
        switch (level)
        {
        case s_debug:
            Log.d(tag, msg);
            break;
        case s_info:
            Log.i(tag, msg);
            break;
        case s_warn:
            Log.w(tag, msg);
            break;
        case s_error:
            Log.e(tag, msg);
            break;
        default:
            Log.d(tag, msg);
            break;
        }
    }
}
